package com.atguigu.yygh.hosp.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.HttpRequestHelper;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.MD5;
import com.atguigu.yygh.hosp.utils.Result;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;
@Api(tags = "医院管理接口")
@RestController
@RequestMapping("api/hosp")
public class ApiController {
     @Autowired
    private HospitalService hospitalService;
     @Autowired
     private HospitalSetService hospitalSetService;

     @Autowired
    DepartmentService departmentService;
     @Autowired
     private ScheduleService scheduleService;


    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request){
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //必填
        String hosScheduleId = (String)paramMap.get("hosScheduleId");

        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }


    @ApiOperation(value = "获取排班分页列表")
    @PostMapping("schedule/list")
    public Result schedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        //非必填
        String depcode = (String)paramMap.get("depcode");
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));

        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);

        Page<Schedule> pageModel = scheduleService.selectPage(page , limit, scheduleQueryVo);



        return Result.ok(pageModel);
    }

    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);
        String mapStr = JSONObject.toJSONString(paramMap);
        Schedule schedule = JSONObject.parseObject(mapStr,Schedule.class);
        System.out.println("1 =2 ");
        scheduleService.save(schedule);
        return Result.ok();
    }


    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        //必须参数校验 略
        String hoscode = (String)paramMap.get("hoscode");
        String depcode = (String)paramMap.get("depcode");

        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

     //分页查询
     @ApiOperation(value = "获取分页列表")
     @PostMapping("department/list")
     public Result department(HttpServletRequest request) {
         Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
         //必须参数校验 略
         String hoscode = (String)paramMap.get("hoscode");
         //非必填
         String depcode = (String)paramMap.get("depcode");
         int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String)paramMap.get("page"));
         int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String)paramMap.get("limit"));
         //签名校验

         DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
         departmentQueryVo.setHoscode(hoscode);
         departmentQueryVo.setDepcode(depcode);
         Page<Department> pageModel = departmentService.selectPage(page, limit, departmentQueryVo);
         return Result.ok(pageModel);
     }
     //上传科室
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> parmMap = HttpRequestHelper.switchMap(parameterMap);
        String map = JSONObject.toJSONString(parmMap);
        Department department = JSONObject.parseObject(map, Department.class);
        departmentService.save(department);
        return Result.ok();

    }

     //查询医院
    @PostMapping("hospital/show")
    public Result showHosp(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

        String hoscode = (String) paramMap.get("hoscode");
        if (hoscode==null){
            throw new YyghException(20001,"医院编号参数错误");
        }

     Hospital hospital = hospitalService.getHospByHoscode(hoscode);
        return Result.ok(hospital);

    }



     @ApiOperation("上传医院信息")
     @PostMapping("saveHospital")
    public Result createHospital(HttpServletRequest request){
         //方法request.getParameterMap();作用获取提交所有参数名称和值封装成map
        //1.获取医院模拟系统传递map集合数据
         Map<String, String[]> parameterMap = request.getParameterMap();
         //调工具类把 Map<String, String[]>封装成Map<String,Obeject>
         Map<String, Object> paramMap = HttpRequestHelper.switchMap(parameterMap);

         //传输过程中"+"转换成" "， 因此我们要转换回来
         String logoData = (String) paramMap.get("logoData");
         logoData = logoData.replaceAll(" ", "+");
         paramMap.put("logoData",logoData);

         //2.从parameterMap获取所有数据添加到Mongdb
         //把map转成对象Hospital
         //JSONObject.toJSONString(map)
         String maps = JSONObject.toJSONString(paramMap);
         //JSONObject.parseObject(JSON字符串，实体类.class)
         Hospital hospital = JSONObject.parseObject(maps, Hospital.class);
//         //签名校验
//         //医院系统传过来的key获取到
         String hospSign = (String) paramMap.get("sign");
//         //从医院平台根据编号查key
         String  signKey =  hospitalSetService.getHospSetSignKey(hospital.getHoscode());
         String encrypt1 = MD5.encrypt(signKey);
//         //医院系统传过来的key和医院平台的key比对
         if (!hospSign.equals(encrypt1)){
             throw new YyghException(20001,"签名校验失败");
         }

//         调service的添加方法
         hospitalService.saveHosp(hospital);
         return Result.ok();
     }

}
